﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
using System.Text;

using CE.User;
using CE.Global;
using CE.Provider;
using CE.CarInsurance;

public partial class provider_ProposalForm_RenewalProposalForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            string[] vals = Request.QueryString["Ref"].ToString().Split(',');
            string prid = "0";
            string pyr = "0";
            string val = "0";
            if (vals.Length == 3)
            {
                val = vals[0].ToString();
                prid = vals[1].ToString();
                pyr = vals[2].ToString();

                Int64 RefId = Convert.ToInt64(val);
                string InsType = Session[General.InsType].ToString();
                Int64 PremiumId = Convert.ToInt64(prid);
                Int32 PaymentYear = Convert.ToInt32(pyr);
                string convertno = General.GetRefNumber(RefId);
                string _fileName = "FileName";
                string _downloaddate = " : " + CE.Global.General.GetUTCDateTime().Day.ToString("00") + "/" + CE.Global.General.GetUTCDateTime().Month.ToString("00") + "/" + CE.Global.General.GetUTCDateTime().Year + " " + CE.Global.General.GetUTCDateTime().ToShortTimeString();
                if (Session[CE.Global.General.ProviderCompanyId].ToString() == "23")
                {
                    if (Session[General.InsType].ToString() == "CI")
                    {
                        lblContent.Text = GenerateCIHTML_Hsbc(RefId, InsType, PremiumId, PaymentYear);
                        _fileName = "H" + convertno + "_" + CE.Global.General.GetUTCDateTime().Day.ToString("00") + "-" + CE.Global.General.GetUTCDateTime().Month.ToString("00") + "-" + CE.Global.General.GetUTCDateTime().Year;
                        StringBuilder strText = new StringBuilder("");
                        strText.Append("<table><tr><td colspan='2'><h3>Car Insurance - Renewal Proposal Form</h3></td><td colspan='2' align='right'><b>Referance No</b> : H" + convertno.ToString() + "</td></tr>");
                        strText.Append("<tr><td><b>Insurer</b></td><td>HSBC Insurance</td><td colspan='2' align='right'><b>Download Date</b> " + _downloaddate.ToString() + "</td></tr></table>");
                        ExportGridView(_fileName, lblContent, strText.ToString());
                    }
                }
            }
        }
    }

    public string GenerateCIHTML_Hsbc(Int64 RefId, string InsuranceType, Int64 PremiumId, Int32 PaymentYear)
    {
        DataSet _dsProposal = new DataSet();
        ProposalDownloadDB _proposaldownloaddb = new ProposalDownloadDB();
        _dsProposal = _proposaldownloaddb.GetHSBCRenewalProposal(RefId,PremiumId, InsuranceType);
        string _html = "";
        if (_dsProposal.Tables[0].Rows.Count > 0)
        {
            #region variables
            string _planname, _annualpremium, _name, _email, _lastyearpolicyno, _nric, _mobile, _vehicleno;
            string _nameofcard, _issuebank, _cno, _ccno, _ccexmm, _ccexyy, _cctype;

            _planname = ""; _annualpremium = ""; _name = ""; _email = ""; _lastyearpolicyno = ""; _nric = ""; _mobile = ""; _vehicleno = "";
            _nameofcard = ""; _issuebank = ""; _cno = ""; _ccno = ""; _ccexmm = ""; _ccexyy = ""; _cctype = "";

            _planname = _dsProposal.Tables[0].Rows[0]["PlanName"].ToString();
            _annualpremium = _dsProposal.Tables[0].Rows[0]["AnnualPremium"].ToString();
            _name = _dsProposal.Tables[0].Rows[0]["FamilyName"].ToString() + " " + _dsProposal.Tables[0].Rows[0]["GivenName"].ToString();
            _email = _dsProposal.Tables[0].Rows[0]["Email"].ToString();
            _lastyearpolicyno = _dsProposal.Tables[0].Rows[0]["LastYearPolicyNo"].ToString();
             _nric = _dsProposal.Tables[0].Rows[0]["NRICNo"].ToString();
            _mobile = _dsProposal.Tables[0].Rows[0]["MobileNo"].ToString();
            _vehicleno = _dsProposal.Tables[0].Rows[0]["VechicleNo"].ToString();

            DataSet _dsPayment = new DataSet();
            _dsPayment = _proposaldownloaddb.GetRenewalPaymentDetails(RefId, 23, InsuranceType, PaymentYear);
            if (_dsPayment != null && _dsPayment.Tables[0].Rows.Count > 0)
            {
                _nameofcard = _dsPayment.Tables[0].Rows[0]["NameoftheCard"].ToString();
                _issuebank = _dsPayment.Tables[0].Rows[0]["IssuingBankName"].ToString();
                _cno = _dsPayment.Tables[0].Rows[0]["CreditCardNo"].ToString();
                _ccexmm = _dsPayment.Tables[0].Rows[0]["CardExpiryMonth"].ToString();
                _ccexyy = _dsPayment.Tables[0].Rows[0]["CardExpiryYear"].ToString();
                _cctype = _dsPayment.Tables[0].Rows[0]["CardType"].ToString();
               if (_dsPayment.Tables[0].Rows[0]["CreditCardNo"].ToString() != "")
                    _ccno = _cno.Substring(0, 4) + "-" + _cno.Substring(4, 4) + "-" + _cno.Substring(8, 4) + "-" + _cno.Substring(12, 4);
                else
                    _ccno = "";
            }
            #endregion

            _html = "<table><tr><td width='25%'><b>Plan Name</b></td><td>" + _planname + "</td><td colspan='2' align='right'><b>Annual Premium</b> : $" + _annualpremium.ToString() + "</td></tr></table>";
            _html += "<div><div><b>Details of Proposer</b></div><div style='text-align: center'>"
                     + "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>"

                     + "<tr><td style='width:20%' align='left'>Name(as shown in NRIC / Passport)</td>"
                     + "<td style='width:30%' align='left'>" + _name.ToString() + "</td>"
                     + "<td style='width:20%' align='left'>NRIC Number</td>"
                     + "<td style='width:30%' align='left'>" + _nric.ToString() + "</td></tr>"

                     + "<tr><td style='width:20%' align='left'>Email Address</td>"
                     + "<td style='width:30%' align='left'>" + _email.ToString() + "</td>"
                     + "<td style='width:20%' align='left'>Mobile No</td>"
                     + "<td style='width:30%' align='left'>" + _mobile.ToString() + "</td></tr>"

                     + "<tr><td style='width:20%' align='left'>Last Year Policy No</td>"
                     + "<td style='width:30%' align='left'>" + _lastyearpolicyno.ToString() + "</td>"
                     + "<td style='width:20%' align='left'>Vehicle No</td>"
                     + "<td style='width:30%' align='left'>" + _vehicleno.ToString() + "</td></tr>";

            ProposalDownloadDB _proposaldb = new ProposalDownloadDB();
            DataSet dsDownload = new DataSet();
            dsDownload = _proposaldb.GetCarRenewalDownloadInfo(RefId, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));

            string isdownloaded = "False";
            if (dsDownload.Tables[0].Rows.Count > 0)
            {
                isdownloaded = dsDownload.Tables[0].Rows[0]["IsConfirmDownload"].ToString();
            }

            if (isdownloaded == "False" || isdownloaded == "0")
            {
                _html += "<tr><td colspan='4' align='left'><b>Payment Details</b></td></tr>"

                + "<tr><td style='width:20%' align='left'>Payment Mode</td>"
                + "<td style='width:30%' align='left'>Credit Card</td>"
                + "<td style='width:20%' align='left'>Type of Card</td>"
                + "<td style='width:30%' align='left'>" + _cctype.ToString() + "</td></tr>"
 
                + "<tr><td style='width:20%' align='left'>Name of the Card</td>"
                + "<td style='width:30%' align='left'>" + _nameofcard.ToString() + "</td>"
                + "<td style='width:20%' align='left'>Issuing Bank</td>"
                + "<td style='width:30%' align='left'>" + _issuebank.ToString() + "</td></tr>"

                + "<tr><td style='width:20%' align='left'>Credit Card No.</td>"
                + "<td style='width:30%' align='left'>" + _ccno.ToString() + "</td>"
                + "<td style='width:20%' align='left'>Card expiry date</td>"
                + "<td style='width:30%; mso-number-format:mmm\\-yyyy' align='left'>" + _ccexmm.ToString() + "/" + _ccexyy.ToString() + "</td></tr>"

                + "</table>"
                + "</td>"
                + "</tr>"

                + "</table>"
                + "</div></div>";
            }
            else
            {
                _html += " "
                + "</table>"
                + "</div></div>";

            }
        }
        return _html;
    }

    public void ExportGridView(string fileName, Label lblContent, string headerText)
    {
        string attachment = "attachment; filename=" + fileName + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        // Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        frm.InnerHtml = headerText;
        lblContent.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(lblContent);
        frm.RenderControl(htw);
        //GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

    public string NoNCD(string NCD)
    {
        string _ncd = "";
        if (NCD == "0")
        {
            _ncd = "10%";
        }
        else if (NCD == "10")
        {
            _ncd = "20%";
        }
        else if (NCD == "20")
        {
            _ncd = "30%";
        }
        else if (NCD == "30")
        {
            _ncd = "40%";
        }
        else if (NCD == "40" || NCD == "50")
        {
            _ncd = "50%";
        }
        return _ncd;
    }

    public string OneTimesNCD(string NCD)
    {
        string _ncd = "";
        if (NCD == "0" || NCD == "10" || NCD == "20" || NCD == "30")
        {
            _ncd = "0%";
        }
        else if (NCD == "40")
        {
            _ncd = "10%";
        }
        else if (NCD == "50")
        {
            _ncd = "20%";
        }
        return _ncd;
    }

}
